from django.db import models


# Create your models here.


class SysUser(models.Model):
    """
        系统用户表
    """
    role_choices = (
        (1, "总监"),
        (2, "部门经理"),
        (3, "员工")
    )
    role = models.IntegerField(choices=role_choices, verbose_name="角色", default=3)
    username = models.CharField(max_length=64, verbose_name='用户名')
    password = models.CharField(max_length=128, verbose_name='密码')
    # 临时方案，存储Token,后续会使用 JWT
    token = models.CharField(max_length=128, null=True, blank=True, verbose_name='TOKEN')
    # 多对一
    department = models.ForeignKey(verbose_name="部门", on_delete=models.CASCADE, to='Department', null=True,
                                   blank=True)
    gender = models.SmallIntegerField(verbose_name="性别", choices=((1, "男"), (0, "女")), null=True, blank=True,
                                      default=1)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间", null=True, blank=True)
    # 多对多
    tags = models.ManyToManyField(to='Tag', verbose_name="标签", blank=True)


class Department(models.Model):
    """
        部门表
    """
    title = models.CharField(max_length=64, verbose_name='部门')
    order = models.IntegerField(default=0, verbose_name='顺序')
    count = models.IntegerField(default=0, verbose_name='人数')


class Tag(models.Model):
    """
    标签表
    """
    caption = models.CharField(max_length=32, verbose_name='标签名称')
    is_active = models.IntegerField(
        choices=((1, "激活"), (0, "禁用")),
        default=1,
        verbose_name='状态')


class NbUserInfo(models.Model):
    """
        人员表2
    """
    name = models.CharField(max_length=64, verbose_name='姓名')
    age = models.IntegerField(verbose_name='年龄')
    gender = models.SmallIntegerField(verbose_name='性别', choices=((1, "男"), (0, "女")), default=1)


class UserInfo(models.Model):
    username = models.CharField(verbose_name="用户名", max_length=32, db_index=True)
    password = models.CharField(verbose_name="密码", max_length=255)
    avatar = models.CharField(max_length=256, verbose_name='头像URL', db_comment='头像URL', default="", null=True,
                              blank=True)
    token = models.CharField(verbose_name="TOKEN", max_length=64, null=True, blank=True, db_index=True)


class Blog(models.Model):
    DoesNotExist = None
    category_choices = ((1, "云计算"), (2, "Python全栈"), (3, "Go开发"), (4, "机器学习"), (5, "前端"))
    category = models.IntegerField(verbose_name="分类", choices=category_choices)

    image = models.CharField(verbose_name="封面", max_length=255)
    title = models.CharField(verbose_name="标题", max_length=32)
    summary = models.CharField(verbose_name="简介", max_length=256)
    text = models.TextField(verbose_name="博文")
    ctime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    creator = models.ForeignKey(verbose_name="创建者", to="UserInfo", on_delete=models.CASCADE)

    comment_count = models.PositiveIntegerField(verbose_name="评论数", default=0)
    favor_count = models.PositiveIntegerField(verbose_name="赞数", default=0)


class Favor(models.Model):
    """ 赞 """
    blog = models.ForeignKey(verbose_name="博客", to="Blog", on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name="用户", to="UserInfo", on_delete=models.CASCADE)
    create_datetime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['blog', 'user'], name='uni_favor_blog_user')
        ]


class Comment(models.Model):
    """ 评论表 """
    DoesNotExist = None
    blog = models.ForeignKey(verbose_name="博客", to="Blog", on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name="用户", to="UserInfo", on_delete=models.CASCADE)

    content = models.CharField(verbose_name="内容", max_length=150)
    create_datetime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    # 新增：支持多级评论
    parent = models.ForeignKey(
        'self',
        verbose_name="父级评论",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='replies'
    )


class Book(models.Model):
    """
    书籍表
    """
    DoesNotExist = None
    title = models.CharField(verbose_name="书名", max_length=32)
    author = models.CharField(verbose_name="作者", max_length=32)
    price = models.DecimalField(verbose_name="价格", max_digits=5, decimal_places=2)
    pub_date = models.DateField(verbose_name="出版日期", null=True, blank=True)
